
<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button style="margin-right: 15px" type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
          <el-upload
              class="upload-demo"
              action=""
              :auto-upload="false"
              :show-file-list="false"
              :on-change="handleChange"
              accept=".xlsx, .xls">
            <el-button type="primary" size="mini" icon="el-icon-plus" disabled>导入</el-button>
          </el-upload>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              max-height="500px"
              :data="tableData"
              row-key="id"
              v-loading="loading"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="plotCode" label="编号"/>
            <el-table-column prop="plotName" label="名称"/>
            <el-table-column prop="address" label="位置信息" />
            <el-table-column prop="chargePerson" label="负责人"/>
            <el-table-column prop="personPhone" label="联系方式"/>
            <el-table-column prop="deviceNum" label="设备数量"/>
            <el-table-column label="状态" key="enabled" width="160px">
              <template #default="scope">
                <el-switch
                    v-model="scope.row.enabled"
                    :active-value="0"
                    active-text="启用"
                    :inactive-value="1"
                    inactive-text="禁用"
                    @change="handleStatusChange(scope.row)"
                />
              </template>
            </el-table-column>
              <el-table-column label="操作" fixed="right" width="150px">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" @click="onDetail(scope.row)">详情</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>

      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
import {CommonStatusEnum} from "@/utils/enum";
export default {
  name: "index",
  components: {
    Add,
  },
  mixins: [crud],
  data() {
    return {
      request:http.plot,
      searchList: [
        {
          label: '编号',
          prop: 'plotCode',
          name: 'input',
          clearable: true
        },
        {
          label: '名称',
          prop: 'plotName',
          name: 'input',
          clearable: true
        },
        {
          label: '位置',
          prop: 'location',
          name: 'input',
          clearable: true
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          options: [
            {
              label: '全部',
              value: ''
            },
            {
              label: '正常',
              value: 1
            },
            {
              label: '停用',
              value: 0
            }
          ]
        }
      ],
      searchParams: {
        plotName: '',
        plotCode: '',
        location: '',
        status: ''
      },
      tableData: [

      ]
    }
  },
  methods: {
    async handleStatusChange(row) {
      try {
        // 发起修改状态
        await http.plot.updateStatus({
          id: row.id,
          enabled: row.enabled
        })
        // 刷新列表
        await this.onSearch()
      } catch {
        // 取消后，进行恢复按钮
        row.enabled =
            row.enabled === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE : CommonStatusEnum.ENABLE
      }
    },
    handleChange() {},
    onDetail(row) {
      this.$router.push({
        path: '/station/detail/' + row.id
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
